<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>添加/编辑巡班事项</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form label-width="100px" ref="editForm">
          <el-form-item label="部门：">
            <el-select v-model="department_id" disabled placeholder="选择部门" size="small">
              <el-option :label="item.name" :value="item.id" v-for="(item, index) in typeList" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="上级类目：">
            <el-select v-model="parent_id" :disabled="disabledStatus" placeholder="选择上级类目" size="small">
              <el-option :label="item.name" :value="item.id" v-for="(item, index) in selectList"
                         :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="巡班事项：">
            <el-input v-model="name" placeholder="巡班事项" size="small"></el-input>
          </el-form-item>
          <el-form-item label="巡班类型：">
            <el-radio-group v-model="radioType">
              <el-radio :label="item.id" v-for="item in typelist">{{ item.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="巡班对象：" v-if="Number(parent_id)">
            <el-radio-group v-model="user_type">
              <el-radio :label="item.id" v-for="item in userList">{{ item.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="序号：">
            <el-input v-model="sort" placeholder="巡班事项" size="small"></el-input>
          </el-form-item>
          <el-form-item label="事项描述：">
            <el-input
                type="textarea"
                placeholder="事项描述"
                v-model="desc"
                maxlength="500"
                size="small"
                rows="4"
                show-word-limit
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click.native="closePOpup" size="small">取消</el-button>
        <el-button size="small" type="primary" @click="checkEditFn">确定保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import dataMenu from "../training/dataMenu.js"
import {classDynamiceAdd, classDynamiceEdit} from "@/api/admin/ClassDynamics"

export default {
  props: ['visible', 'optionData', 'selectList', 'activeName', 'optionType'],
  components: {},
  data() {
    return {
      dialogVisible: false,
      name: '',
      desc: '',
      radioType: 1,
      typelist: [
        {
          id: 1,
          name: '班级'
        },
        {
          id: 2,
          name: '公共区域'
        },
        {
          id: 3,
          name: '通用'
        }
      ],
      userList: [
        {
          name: '幼儿',
          id: 1
        },
        {
          name: '教师',
          id: 2
        },
        {
          name: '生活老师',
          id: 4
        },
        {
          name: '场地',
          id: 3
        }
      ],
      typeList: dataMenu.data,
      department_id: '',
      parent_id: 0,
      disabledStatus: false,
      user_type: 1,
      sort: ''
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        this.department_id = Number(this.activeName)
        if (this.optionType === 'edit') {
          this.disabledStatus = true
        }
        console.log('内容判断：', this.optionData, this.optionType) // 有值是添加二级类目
        if (this.optionData) {
          if (this.optionType === 'add') {
            this.parent_id = this.optionData.id
          }
          this.disabledStatus = true
        }
        // if(this.optionType==='add' && !this.optionData){
        //   this.parent_id = this.optionData.parent_id
        // }
        if (this.optionType === 'edit') {
          this.parent_id = this.optionData ? this.optionData.parent_id : 0
          this.user_type = this.optionData ? this.optionData.user_type : 1
          this.name = this.optionData ? this.optionData.name : ''
          this.desc = this.optionData ? this.optionData.desc : ''
          this.sort = this.optionData ? this.optionData.sort : ''
        }
      } else {
        this.user_type = 1
        this.name = ''
        this.desc = ''
        this.disabledStatus = false
        this.department_id = ''
        this.sort = ''
      }
    }
  },
  created() {
    console.log(dataMenu)
  },
  methods: {
    checkEditFn() {
      // name 选项名称
      // parent_id 上级id
      // department_id 部门id
      // desc 简介
      if (!this.name) {
        this.$message.error('请填写巡班事项');
        return
      }
      let data = {
        name: this.name,
        desc: this.desc,
        type: this.radioType,
        parent_id: this.parent_id,
        department_id: this.department_id,
        user_type: this.parent_id ? this.user_type : 0
      }
      if (this.optionType === 'add') {
        classDynamiceAdd(data).then(res => {
          console.log('res:', res)
          this.$emit('InitFn')
          this.closePOpup()
        })
      } else {
        data.id = this.optionData.id
        classDynamiceEdit(data).then(res => {
          console.log('res:', res)
          this.$emit('InitFn')
          this.closePOpup()
        })
      }
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 500px;
  height: auto;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.resruitment-title {
  padding: 5px 0;
  border-bottom: 2px solid #abd3f3;
  margin-bottom: 20px;
}

.el-radio {
  margin-right: 20px;
}

.jobTime {
  display: flex;
  align-items: center;
}

.jobTime span {
  padding: 0 10px;
}
</style>


